import { ClassService } from '@/services/class';
import { ClrService } from '@/services/classroom';
import { ActionType } from '@ant-design/pro-components';
import { useModel } from '@umijs/max';
import { message } from 'antd';
import { useCallback, useMemo, useRef } from 'react';

export default () => {

  const { classMajor } = useModel('dic');
  const classMajorEnum = useMemo(() => {
    const result = new Map();
    result.set(0, { text: '- please choose -' });
    classMajor.forEach((item) =>
      result.set(item.dic_id, { text: item.dic_name }),
    );
    return result;
  }, [classMajor]);
    console.log(classMajorEnum);
  const teacherList=useCallback(async()=>{
    return ClassService.categoryStaffList(4)
  },[])
  const adminList=useCallback(async()=>{
    return ClassService.categoryStaffList(5)
  },[])
  const jobList=useCallback(async()=>{
    return ClassService.categoryStaffList(6)
  },[])
    
    
  const actionRef = useRef<ActionType>();
  const getClrHandler = useCallback(async () => {
    return ClrService.getClrList();
  },[])

  const getHandler = useCallback(async (body: API.Class_List_Body) => {
    return ClassService.getClassList(body);
  }, []);

  const addHandler = useCallback(async (body: API.Class_Add_Body) => {
    const hide = message.loading('dismissing..');
    try {
      await ClassService.addClass(body);
      hide();
      message.success(`新增成功！`);
      actionRef.current?.reload?.();
      return true;
    } catch (error) {
      hide();
      message.error(`新增失败，请重试！`);
      return false;
    }
  }, []);
  const updateHandler = useCallback(async (body: API.Class_Update_Body) => {
    const hide = message.loading('dismissing..');
    try {
      await ClassService.updateClass(body);
      hide();
      message.success(`修改成功！`);
      actionRef.current?.reload?.();
      return true;
    } catch (error) {
      hide();
      message.error(`修改失败，请重试！`);
      return false;
    }
  }, []);
  const beginHandler = useCallback(async (body: API.Class_Begin_Body) => {
    const hide = message.loading('dismissing..');
    try {
      await ClassService.beginClass(body);
      hide();
      message.success(`开课成功！`);
      actionRef.current?.reloadAndRest?.();
      return true;
    } catch (error) {
      hide();
      message.error(`开课失败，请重试！`);
      return false;
    }
  }, []);
  const endHandler = useCallback(async (body: API.Class_End_Body) => {
    const hide = message.loading('dismissing..');
    try {
      await ClassService.endClass(body);
      hide();
      message.success(`结课成功！`);
      actionRef.current?.reloadAndRest?.();
      return true;
    } catch (error) {
      hide();
      message.error(`结课失败，请重试！`);
      return false;
    }
  }, []);
  
  return {
    classMajorEnum,
    classMajor,
    actionRef,
    getHandler,
    addHandler,
    updateHandler,
    beginHandler,
    endHandler,
    getClrHandler,
    teacherList,
    adminList,
    jobList,
  };
};
